import pyglet
from pyglet.gl import *
window = pyglet.window.Window(visible=False, resizable=True)

def on_resize(width, height):
	"""Setup 3D viewport"""
	glViewport(0, 0, width, height)
	glMatrixMode(GL_PROJECTION)
	glLoadIdentity()
	gluPerspective(70, 1.0*width/height, 0.1, 1000.0)
	glMatrixMode(GL_MODELVIEW)
	glLoadIdentity()
window.on_resize = on_resize
window.set_visible()

xrot = yrot = 0
zoom = -2.5

@window.event
def on_draw():
	window.clear()
	glLoadIdentity()
	glTranslatef(0, 0, zoom)
	glRotatef(xrot, 1, 0, 0)
	glRotatef(yrot, 0, 1, 0)
	glBegin(GL_QUADS)
	glColor3f(1.0, 0, 0)
	glVertex3f(1.0, -1.0, 0.0)
	glColor3f(1.0, 1.0, 0)
	glVertex3f(1.0, 1.0, 0.0)
	glColor3f(0, 1.0, 0)
	glVertex3f(-1.0, 1.0, 0.0)
	glColor3f(0, 0, 1.0)
	glVertex3f(-1.0, -1.0, 0.0)
	glEnd()

@window.event
def on_mouse_motion(x, y, dx, dy):
	global xrot, yrot
	yrot += dx * 0.3
	xrot += dy * 0.3

@window.event
def on_mouse_scroll(x, y, scroll_x, scroll_y):
	global zoom
	zoom -= scroll_y * 0.02

window.set_exclusive_mouse()
pyglet.app.run()
